import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        {path: '/', redirect: '/index'},
        {path: '/index', name: 'index', component: () => import('@/components/Index')},
        {path: '/classify', name: 'classify', redirect:'/classify/recommend/1005000', component: () => import('@/components/Classify'),
            children: [
                {path: 'recommend/:id', name: 'recommend', component: () => import('@/components/ClassifyChildrenPage/Recommend')}
            ]},
        {path: '/shoppingcart', name: 'shoppingcart', meta: {auth: true}, component: () => import('@/components/ShoppingCart')},
        {path: '/user', name: 'user', component: () => import('@/components/User')},
        {path: '/login', name: 'login', component: () => import('@/components/UserChildrenPage/Login')},
        {path: '/register', name: 'register', component: () => import('@/components/UserChildrenPage/Reigster')},
        {path: '/orders', name: 'orders', redirect: '/orders/allorders', component: () => import('@/components/UserChildrenPage/Orders'),
            children: [
                {path: 'allorders', name: 'allorders', component: () => import('@/components/UserChildrenPage/AllOrders')},
                {path: 'obligation', name: 'obligation', component: () => import('@/components/UserChildrenPage/Obligation')},
                {path: 'awaitshipments', name: 'awaitshipments', component: () => import('@/components/UserChildrenPage/AwaitShipments')},
                {path: 'awaitreceiving', name: 'awaitreceiving', component: () => import('@/components/UserChildrenPage/AwaitReceiving')},
                {path: 'evaluate', name: 'evaluate', component: () => import('@/components/UserChildrenPage/Evaluate')}
            ]
        },
        {path: '/address', name: 'address', component: () => import('@/components/UserChildrenPage/Address')},
        {path: '/additionadd', name: 'additionadd', component: () => import('@/components/UserChildrenPage/AdditionAdd')},
        {path: '/search', name: 'search', component: () => import('@/components/Search')},
        {path: '/goodsmsg', name: 'goodsmsg', component: () => import('@/components/GoodsPages/GoodsMsg')},
        {path: '/goodslist', name: 'goodslist', component: () => import('@/components/GoodsList')},
        {path: '/collect', name: 'collect', meta:{auth: true}, component: () => import('@/components/UserChildrenPage/Collect')},
        {path: '/suborder', name: 'suborder', component: () => import('@/components/GoodsPages/SubOrder')},
        {path: '/setting', name: 'setting', meta:{auth: true}, component: () => import('@/components/UserChildrenPage/Setting')},
        {path: '/resetting', name: 'resetting', component: () => import('@/components/UserChildrenPage/Resetting')},
        {path: '/updateaddress', name: 'updateaddress', component: () => import('@/components/UserChildrenPage/UpdateAddress')}
    ],
    linkExactActiveClass: 'active',
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const token = sessionStorage.getItem('token')
    if (!token && to.meta.auth) {
        return next('/login')
    }
    next()
})


export default router
